<?php
class IngresoModel extends ModelBase
{
	public function cambia_tipo_ingreso_empresa()
	{
		include("config.php");
		
		// INGRESOS MES ACTUAL	
		$mes_curso = date("Y")."-".date("m")."-01";
		$mes = 1;
		$calculo = strtotime("$mes_curso +$mes month");
		$mes_curso_a = date("Y-m-d", $calculo);

		$select = " c.id_empresa id_empresa, COUNT(*) cant ";
		$from = " gc_ingreso i, gc_contratista c, gc_empresa e  ";
	  	$where = " c.vigente = 'S'
				and i.id_contratista = c.id
				and c.id_empresa = e.id
				and e.id_tipo_ingreso = 1
				and i.fecha >= '".$mes_curso."' 
				and i.fecha < '".$mes_curso_a."' ";	
		$where .= " GROUP BY c.id_empresa HAVING COUNT(*) > 0";
		
		$sqlpersonal = new SqlPersonalizado($config->get('dbhost'), $config->get('dbuser'), $config->get('dbpass') );
		$sqlpersonal->set_select($select); 
	  	$sqlpersonal->set_from($from);
		$sqlpersonal->set_where($where);
	
    	$sqlpersonal->load();
		
		$coleccion = $sqlpersonal;
				
		$in = "";
		for($j=0; $j<$coleccion->get_count(); $j++) 
		{
			$datoTmp = &$coleccion->items[$j];
			if($in == "")
			{
				$in = $datoTmp->get_data("id_empresa");
			}
			else
			{
				$in .= ",".$datoTmp->get_data("id_empresa");
			}
		}
		
		if($in <> "")
		{
			$mes_curso = date("Y")."-".date("m")."-01";
			$mes = 1;
			$calculo = strtotime("$mes_curso -$mes month");
			$mes_curso_a = date("Y-m-d", $calculo);
			
			$select = " c.id_empresa id_empresa, COUNT(*) cant ";
			$from = " gc_ingreso i, gc_contratista c, gc_empresa e  ";
			$where = " c.vigente = 'S'
					and i.id_contratista = c.id
					and c.id_empresa = e.id
					and e.id_tipo_ingreso = 1
					and i.fecha >= '".$mes_curso_a."' 
					and i.fecha < '".$mes_curso."' ";	
			if($in <> "")
			{
				$where .= " and c.id_empresa in (".$in.")";
			}
			$where .= " GROUP BY c.id_empresa HAVING COUNT(*) > 0";
			
			$sqlpersonal = new SqlPersonalizado($config->get('dbhost'), $config->get('dbuser'), $config->get('dbpass') );
			$sqlpersonal->set_select($select); 
			$sqlpersonal->set_from($from);
			$sqlpersonal->set_where($where);
			$sqlpersonal->load();
			
			$coleccion = $sqlpersonal;
			
			$in = "";
			for($j=0; $j<$coleccion->get_count(); $j++) 
			{
				$datoTmp = &$coleccion->items[$j];
				if($in == "")
				{
					$in = $datoTmp->get_data("id_empresa");
				}
				else
				{
					$in .= ",".$datoTmp->get_data("id_empresa");
				}
			}
		
			if($in <> "")
			{
				$mes_curso = $mes_curso_a; 
				$mes = 1;
				$calculo = strtotime("$mes_curso -$mes month");
				$mes_curso_a = date("Y-m-d", $calculo);
				
				$select = " c.id_empresa id_empresa, COUNT(*) cant ";
				$from = " gc_ingreso i, gc_contratista c, gc_empresa e  ";
				$where = " c.vigente = 'S'
						and i.id_contratista = c.id
						and c.id_empresa = e.id
						and e.id_tipo_ingreso = 1
						and i.fecha >= '".$mes_curso_a."' 
						and i.fecha < '".$mes_curso."' ";	
				if($in <> "")
				{
					$where .= " and c.id_empresa in (".$in.")";
				}
				$where .= " GROUP BY c.id_empresa HAVING COUNT(*) > 0";
								
				$sqlpersonal = new SqlPersonalizado($config->get('dbhost'), $config->get('dbuser'), $config->get('dbpass') );
				$sqlpersonal->set_select($select); 
				$sqlpersonal->set_from($from);
				$sqlpersonal->set_where($where);
				$sqlpersonal->load();
				
				$coleccion = $sqlpersonal;
				
				for($j=0; $j<$coleccion->get_count(); $j++) 
				{
					
					$datoTmp = &$coleccion->items[$j];
					
					$empresa = new Empresa();
					$empresa->add_filter("id","=",$datoTmp->get_data("id_empresa"));
					$empresa->load();
					$empresa->set_data("id_tipo_ingreso",2); // PERMANENTE
					$empresa->save();			
				}
			}
		} // fin if primer in <> de vacio
	}
}	
?>